package sklad;

import data.DataHelper;
import data.ExtramobiExcel;
import data.ExtramobiNew;
import data.ItemData;
import jxl.read.biff.BiffException;
import obj.Item;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;

/**
 * User: Vladimir
 * Date: 13.01.13 Time: 13:29
 */
public class AllGoodsManager {
    public static void main(String[] args) throws IOException, BiffException {
/*
        Map<String, List<ItemData>> allItems = getAllItemData();
        for (String category : allItems.keySet()) {
            System.out.println(category + ":");
            List<ItemData> items = allItems.get(category);
            for (ItemData item : items) {
                System.out.println(item.article + " - " + item.name);
            }
        }
*/
        printCraftmannModels();
    }

    public static Map<String, List<ItemData>> getAllItemData() throws BiffException, IOException, IllegalAccessException {
        String basePath = "data/extramobi";
        Map<String, List<ItemData>> allItems = getItemDataNew(basePath);
        Map<String, List<ItemData>> oldItems = getItemDataOld(basePath);
        allItems.putAll(oldItems);
        return allItems;
    }

    private static Map<String, List<ItemData>> getItemDataOld(String basePath) throws BiffException, IOException {
        Map<String, List<ItemData>> allItems = new HashMap<String, List<ItemData>>();
        File baseFolder = new File(basePath);
        for (File categoryFolder : baseFolder.listFiles()) {
            String categoryName = categoryFolder.getName();
            if (categoryName.contains(".") ||
                    categoryFolder.isFile()) {
                continue;
            }
            List<ItemData> itemDataList = new ArrayList<ItemData>();
            String categoryFilePath = categoryFolder.getPath() + "/" + categoryName + ".xls";
            if (!new File(categoryFilePath).isFile()) {
                String prefix = "";
                if (categoryName.equalsIgnoreCase("accum-craft")) {
                    prefix = "Craftmann ";
                }
                List<Item> items = Item.parseItemsXlsDir(categoryFolder.getPath() + "/");
                String propsFile = categoryFolder.getPath() + "\\" + categoryName + ".properties";
                for (Item item : items) {
                    ItemData itemData = new ItemData();
                    Map<String, String> props = DataHelper.readProps(DataHelper.Props.ITEM, propsFile);
                    ExtramobiNew.filterProp(props, "item.name", "\\$\\{item\\.mah\\}", ExtramobiNew.getMah(item.props));
                    ExtramobiNew.filterProp(props, "item.name", "\\$\\{item\\.name\\}", prefix + item.name);
                    String name = props.get("item.name");
                    itemData.article = item.article;
                    itemData.name = name;
                    itemData.brand = item.brand;
                    itemData.price = item.price;
                    itemDataList.add(itemData);
                }
                allItems.put(categoryName, itemDataList);
            }
        }
        return allItems;
    }

    private static Map<String, List<ItemData>> getItemDataNew(String basePath) throws IOException, IllegalAccessException {
        Map<String, List<ItemData>> allItems = new HashMap<String, List<ItemData>>();
        File baseFolder = new File(basePath);
        for (File categoryFolder : baseFolder.listFiles()) {
            String categoryName = categoryFolder.getName();
            if (categoryName.contains(".") ||
                    categoryFolder.isFile()) {
                continue;
            }
            String categoryFilePath = categoryFolder.getPath() + "/" + categoryName + ".xls";
            if (!categoryName.equals("remont") && new File(categoryFilePath).isFile()) {
                Map<String, List<ItemData>> data = ExtramobiExcel.parseItemData(categoryFilePath);
                for (String brandCategory : data.keySet()) {
                    String category = brandCategory.substring(0, brandCategory.indexOf("-"));
                    if (allItems.containsKey(category)) {
                        List<ItemData> items = allItems.get(category);
                        for (ItemData newItem : data.get(brandCategory)) {
                            if (!items.contains(newItem) && !newItem.category) {
                                items.add(newItem);
                            }
                        }
                    } else {
                        List<ItemData> items = new ArrayList<ItemData>();
                        for (ItemData newItem : data.get(brandCategory)) {
                            if (!newItem.category) {
                                items.add(newItem);
                            }
                        }
                        allItems.put(category, items);
                    }
                }
            }
        }
        return allItems;
    }

    public static void printCraftmannModels() throws IOException, BiffException {
        List<Item> items = Item.parseItemsXlsDir("data/extramobi/accum/");
        String path = "data/temp/craft/";
        File folder = new File(path);
        Map<String, List<String>> models = new TreeMap<String, List<String>>();
        List<String> accums = new ArrayList<String>();
        for (File brandFile : folder.listFiles()) {
            String brandName = brandFile.getName().split("\\.")[0];
            HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(brandFile));
            HSSFSheet sheet = wb.getSheetAt(0);
            for (Row row : sheet) {
                if (row.getRowNum() < 2) {
                    continue;
                }
                String brandModel = brandName + " " + Parser.getStringValue(row.getCell(1));
                brandModel = brandModel.toLowerCase();
                String modelName = row.getCell(0).getStringCellValue();
//                modelName = modelName.replace(brandName.toUpperCase() + " ", "");
                modelName = filterModel(modelName);
                modelName = modelName.toLowerCase();
                modelName = modelName.replace(brandName, "").trim();
                if (models.containsKey(brandModel)) {
                    models.get(brandModel).add(modelName);
                } else {
                    List<String> modelList = new ArrayList<String>();
                    modelList.add(modelName);
                    models.put(brandModel, modelList);
                }
                if (!accums.contains(brandModel)) {
                    accums.add(brandModel);
                }
            }
        }
        for (Item item : items) {
            if (models.keySet().contains(item.name.toLowerCase())) {
                System.out.println(item.name);
                List<String> modelList = models.get(item.name.toLowerCase());
                List<String> itemModels = toLowerCase(item.models);
                for (String model : modelList) {
                    if (itemModels.contains(model)) {

                    } else {
                        System.out.print(model + ",");
                    }
                }
                System.out.println();
            } else {
                System.out.println("Unknown item: " + item.name);
            }
        }
        System.out.println("**************************************");
        label :
        for (String accum : accums) {
            for (Item item : items) {
                if (item.name.toLowerCase().equals(accum)) {
                    continue label;
                }
            }
            System.out.print(accum + " : ");
            for (String model : models.get(accum)) {
                System.out.print(model + ",");
            }
            System.out.println();
        }
//        for (String brandModel : models.keySet()) {
//            System.out.print(brandModel + " : ");
//            for (String model : models.get(brandModel)) {
//                model = filterModel(model);
//                System.out.print(model + ",");
//            }
//            System.out.println();
//        }
    }

    private static List<String> toLowerCase(List<String> models) {
        List<String> lowerModels = new ArrayList<String>();
        for (String model : models) {
            lowerModels.add(model.toLowerCase());
        }
        return lowerModels;
    }

    private static String filterModel(String model) {
        model = model.replace("SGH-", "");
        model = model.replace("GT-", "");
        model = model.replace("SCH-", "");
        model = model.replace("SPH-", "");
        String[] fields = model.split("\\s");
        StringBuilder newModel = new StringBuilder();
        for (String field : fields) {
            field = field.toLowerCase();
            char[] fieldsChars = field.toCharArray();
            fieldsChars[0] = Character.toUpperCase(field.charAt(0));
            field = new String(fieldsChars);
            newModel.append(" ").append(field);
        }
        newModel.deleteCharAt(0);
        return newModel.toString();
    }
}
